import axios from 'axios';

const request = axios.create({
    baseURL: 'http://localhost:8090',
    timeout: 5000
});

// 请求拦截器
request.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=utf-8';
    return config;
}, error => {
    return Promise.reject(error);
});

// 响应拦截器
request.interceptors.response.use(
    response => {
        let res = response.data;
        if (response.config.responseType === 'blob') {
            return res;
        }
        if (typeof res === 'string') {
            res = res ? JSON.parse(res) : res;
        }
        return res;
    },
    error => {
        console.log('err', error);
        return Promise.reject(error);
    }
);

// 封装 GET 和 POST 请求方法
export const getRequest = (url, params) => request.get(url, { params });
export const postRequest = (url, data) => request.post(url, data);
export default request;
